package com.beilang.hotline.user.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.beilang.base.core.dao.PaginationBaseDAO;
import com.beilang.base.pagination.PaginationHelper;
import com.beilang.base.util.StringUtil;
import com.beilang.hotline.user.model.UserModel;
import com.beilang.hotline.user.to.SearchTO;

public class UserDAO extends PaginationBaseDAO {
	
	public UserModel createUser(UserModel userModel) {
        userModel.setDeleteInd("N");
        return (UserModel)this.save(userModel);
	}
	
	public UserModel saveUser(UserModel userModel) {
		return (UserModel)this.save(userModel);
	}
	
	public void deleteUser(String userId){
	    UserModel user= this.getUser(userId);
        user.setDeleteInd("Y");
        this.save(user);
	}
	
	public UserModel getUser(String userId){
		return (UserModel)this.get(UserModel.class, userId);
    }
	
	public Integer searchCount(SearchTO searchTO) {
        Map map = new HashMap();       
        String userId=StringUtil.convertEmpty2Null(searchTO.getUserId());
        if(userId!=null)userId="%"+userId+"%";
        String userName=StringUtil.convertEmpty2Null(searchTO.getUserName());
        if(userName!=null)userName="%"+userName+"%";
        String userRole=StringUtil.convertEmpty2Null(searchTO.getUserRole());
        String deptId=StringUtil.convertEmpty2Null(searchTO.getUserDeptId());
        map.put("userId", userId);
        map.put("userName", userName);
        map.put("userRole", userRole);
        if(deptId!=null&&!deptId.equalsIgnoreCase("0")){
            map.put("deptId", deptId);
        }          
        return (Integer) getSqlMapClientTemplate().queryForObject("searchUserCount", map);
    }

    public List searchList(PaginationHelper helper, SearchTO searchTO) {
        Map map = new HashMap();
        String userId=StringUtil.convertEmpty2Null(searchTO.getUserId());
        if(userId!=null)userId="%"+userId+"%";
        String userName=StringUtil.convertEmpty2Null(searchTO.getUserName());
        if(userName!=null)userName="%"+userName+"%";
        String userRole=StringUtil.convertEmpty2Null(searchTO.getUserRole());
        String deptId=StringUtil.convertEmpty2Null(searchTO.getUserDeptId());
        map.put("userId", userId);        
        map.put("userName", userName);
        map.put("userRole", userRole);
        if(deptId!=null&&!deptId.equalsIgnoreCase("0")){
            map.put("deptId", deptId);    
        }
        map = constructPaginationMap(helper, map);
        return getSqlMapClientTemplate().queryForList("searchUserList", map);
    }

     public List searchList(SearchTO searchTO) {
        Map map = new HashMap();
        String userId=StringUtil.convertEmpty2Null(searchTO.getUserId());
        if(userId!=null)userId="%"+userId+"%";
        String userName=StringUtil.convertEmpty2Null(searchTO.getUserName());
        if(userName!=null)userName="%"+userName+"%";
        String userRole=StringUtil.convertEmpty2Null(searchTO.getUserRole());
        String deptId=StringUtil.convertEmpty2Null(searchTO.getUserDeptId());
        map.put("userId", userId);
        map.put("userName", userName);
        map.put("userRole", userRole);
        if(deptId!=null&&!deptId.equalsIgnoreCase("0")){
            map.put("deptId", deptId);
        }
        return getSqlMapClientTemplate().queryForList("searchUserAllList", map);
    }
    
    protected  String mapSortField(String dataField){
    	if(StringUtil.isNullOrBlank(dataField))return dataField;
    	if(dataField.equalsIgnoreCase("userId")) return "user_id";
    	if(dataField.equalsIgnoreCase("userName")) return "user_name";
    	if(dataField.equalsIgnoreCase("deptName")) return "dept_id";        
        if(dataField.equalsIgnoreCase("userRoleDesc")) return "role";
    	if(dataField.equalsIgnoreCase("pcName")) return "pcname";
    	if(dataField.equalsIgnoreCase("channelNo")) return "channelno";
    	if(dataField.equalsIgnoreCase("emailAddress")) return "email";
    	if(dataField.equalsIgnoreCase("telephone")) return "telephone";
    	if(dataField.equalsIgnoreCase("recordMode")) return "record_mode";        
        return dataField;
    }

    public List getUserListByChannel(String channelNo){
        Map map = new HashMap();       
        map.put("channelNo", channelNo);
        return getSqlMapClientTemplate().queryForList("getUserListByChannel", map);
    }
}
